<?php

namespace app\admin\controller\yunfood\food;

use app\admin\model\yunfood\Food;
use app\admin\model\yunfood\Food as GoodsModel;
use app\admin\model\yunfood\FoodCategory;
use app\admin\model\yunfood\Shop;
use addons\yunfood\library\Backend;
use think\Exception;

/**
 * 菜品
 */
class Main extends Backend
{
    /**
     * @var GoodsModel
     */
    protected $model;

    protected $shopField = 'shop_ids';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new GoodsModel();

        // 关联门店
        $this->view->assign('shopId', $this->shopId);
        $this->assignconfig('shopId', $this->shopId);

        // 门店列表
        $shopList = Shop::getOptionList($this->getShopWhere(null, 'id'), true, false);
        $this->view->assign('shopList', $shopList);
        $this->assignconfig('shopList', $shopList);

        // 分类
        $categoryList = FoodCategory::getOptionList();
        $this->view->assign('categoryList', $categoryList);
        $this->assignconfig('categoryList', $categoryList);

        // 状态
        $this->view->assign('statusList', $this->model::getStatusList());
        $this->assignconfig('statusList', $this->model::getStatusList());
    }

    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            // 转发到selectGoods
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            // 重载查询条件
            $filter = $this->request->get('filter');
            $op = $this->request->get('op');
            list($filter, $op) = $this->formatFilterOp($filter, $op);
            $this->request->get(['filter' => $filter, 'op' => $op]);

            // 通用查询条件
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            // 查询门店
            $where = $this->getShopWhere($where, 'shop_ids');
            $list = $this->model->with(['category_data'])
                ->where($where)->order($sort, $order)->limit($offset, $limit)->paginate($limit);
            foreach ($list->items() as $item) {
                $item->append(['shop_text']);
                if (! $this->superAdmin) {
                    if (empty($item->shop_ids) || strpos($item->shop_ids, ',') !== false) {
                        $item['mute_operate'] = true;
                    }
                }
            }
            $result = array(
                'total' => $list->total(),
                'rows'  => $list->items(),
            );

            return json($result);
        }

        return $this->view->fetch();
    }

    /**
     * 格式化查询条件
     *
     * @param $filter
     * @param $op
     * @return array
     */
    private function formatFilterOp($filter, $op)
    {
        $filter = json_decode($filter, true);
        $op = json_decode($op, true);
        if (array_key_exists('shopId', $filter)) {
            if ($filter['shopId'] == 0) {
                $filter['shop_ids'] = '';
                $op['shop_ids'] = '=';
            } else {
                $filter['shop_ids'] = $filter['shopId'] . '';
                $op['shop_ids'] = 'find_in_set';
            }
            unset($filter['shopId'], $op['shopId']);
        }

        return [json_encode($filter), json_encode($op)];
    }

    public function recyclebin()
    {
        return;
    }

    public function destroy($ids = null)
    {
        return;
    }

    public function restore($ids = null)
    {
        return;
    }
}
